package cheme.ProcessModel.Interpreter;

import java.util.ArrayList;
import java.util.Iterator;

import cheme.ProcessModel.HelloModel;
import cheme.ProcessModel.IChild;
import cheme.ProcessModel.IModelElement;
import cheme.ProcessModel.ModelEnv;
import cheme.ProcessModel.ProcessModelConstants;
//import cheme.ProcessModel.ModelEnv;
import cheme.ProcessModel.ProcessModel;
//import cheme.ProcessModel.ProcessModelConstants;
//import cheme.ProcessModel.Interpreter.IRule.RuleResult;
import cheme.ProcessModel.Annotations.Annotation;
import cheme.ProcessModel.Interpreter.IRule.RuleResult;
import cheme.ProcessModel.ProcessUnits.ProcessUnit;
import cheme.ProcessModel.ProcessUnits.TerminalSink;
import cheme.ProcessModel.ProcessUnits.Streams.Stream;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.AbstractTagComponent;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.IGeneralTagComponent;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.ITagComponent;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.OverallTagComponent;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.Tag;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.TagComponentList;

class JMyStruct {
    String  name;
    int     nbr;
};




// This rule checks that Incoming qty equals outgoing qty in the diagram.

public abstract class ValidateDiagram implements IRule{
	
	private ArrayList<IChild> myInstreamList = new ArrayList<IChild>();
	private ArrayList<IChild> myOutstreamList = new ArrayList<IChild>();
	private ArrayList<IChild> myOverallInstreamList = new ArrayList<IChild>();
	private ArrayList<IChild> myOverallOutstreamList = new ArrayList<IChild>();
	private final int UNITS_NOT_SAME = 0;
	private final int QTY_NOT_SAME = 1;
	private final int SUCCESS = 2;
	private final int FAILURE = 3;

	
	
	//protected abstract boolean OverllAllCheck(ArrayList<IChild>list1,ArrayList<IChild>list2);
	//boolean ComponentCheck(ArrayList<IChild>list1,ArrayList<IChild>list2);
	
	private AbstractTagComponent msgObject = null;
	protected Interpreter interpreter = null;
	
	public RuleResult applyTo(IModelElement object)
	{
		RuleResult result = new IRule.Success((IModelElement)object); 
		//ModelCheckCmd
		//IModelElement object;
		if(!(object instanceof ProcessModel))
		{
			object = object.getRoot();
		}
		
		if(object instanceof ProcessModel)
		{
			ProcessModel pm = (ProcessModel)object;
			if(3 < pm.size())
			{
				//result = new IRule.Warning((IModelElement)object, "This is Validate Diagram Rule!!!");
				Iterator<IChild> itr = pm.iterator();
				itr.next();// skip first element, equation editor
				itr.next();// skip second element, Console view
				itr.next();// skip third element ToolTip
				//HelloModel hm = (HelloModel)itr.next(); // skip the second hello model element// skip the second element as well, if Solvability status is added
				//hm.setText("handle");
				while(itr.hasNext())//&& result instanceof IRule.Success)
				{
					String strLabel = "";
					// Set annotation on the desired model element
					if(null != msgObject){
						strLabel = msgObject.getLabel();
						setAnnotation(result, strLabel);
					}
					
					ProcessUnit pu = (ProcessUnit) itr.next();
					SetInStreamList(pu);
					SetOutStreamList(pu);
					if( !(myOverallInstreamList.isEmpty() ||  myOverallOutstreamList.isEmpty()) && !(pu instanceof TerminalSink))
					{
						// Check if the contents and quantities of the Overall list are same.
						// FuctionCAll
						int ret_type = OverllAllCheck(myOverallInstreamList,myOverallOutstreamList);
						if( ret_type == SUCCESS)
						{
							result = new IRule.Warning((IModelElement)msgObject, "OverAll Quantities are same!");
							if(null != myInstreamList && null != myOutstreamList)
							{
								if(myInstreamList.isEmpty() && myOutstreamList.isEmpty()){
									result = new IRule.Success((IModelElement)msgObject);
									//break;
									continue;
								}
								// Input stream is empty but output stream is NOT
								else if((myInstreamList.isEmpty() && !myOutstreamList.isEmpty()))
								{
									String listofNames  = GetStreamElementNames(myOutstreamList);
									result = new IRule.Warning((IModelElement)msgObject, "Outgoing stream(s) contain\n"+ listofNames+ "\n which are NOT specified in the incoming stream(s). \n  Make sure that every compound that leaves a processing unit also enters that unit. ");
									break;
								}
								// if incoming stream has elements but outgoing stream is empty
								else if(!myInstreamList.isEmpty() && myOutstreamList.isEmpty())
								{
									String listofNames  = GetStreamElementNames(myInstreamList);
									result = new IRule.Warning((IModelElement)msgObject, "Incoming stream(s) contain\n"+ listofNames+ "\n which are NOT specified in the outgoing stream(s).  \n Make sure that every compound that enters a processing unit also leaves that unit.");
									break;
								}
								// Check for the elements present in the incoming stream but not present in the outgoing streams.
								String listofNames = CheckelelementInOutlist(myInstreamList, myOutstreamList);
								if(null != listofNames && !listofNames.isEmpty()){
									result = new IRule.Warning((IModelElement)msgObject, "Incoming stream(s) contain\n"+ listofNames+ "\n which are NOT specified in the outgoing stream(s).  \n Make sure that every compound that enters a processing unit also leaves that unit.");
									break;
								}
								// Elements present in Out stream but nor present in Incoming stream
								listofNames = CheckelelementInOutlist(myOutstreamList,myInstreamList);
								if(null != listofNames && !listofNames.isEmpty()){
									result = new IRule.Warning((IModelElement)msgObject, "Outgoing stream(s) contain\n"+ listofNames+ "\n which are NOT specified in the incoming stream(s). \n  Make sure that every compound that leaves a processing unit also enters that unit. ");
									break;
								}
								// Check if the contents and quantities of the two list are same.	
								int retval = ComponentCheck(myInstreamList,myOutstreamList);
								switch(retval)
								{
									case UNITS_NOT_SAME:
									{
										result = new IRule.Warning((IModelElement)msgObject, "Overall mass balance is satisfied.\n The units attached to the quantity of one or more of the individual compounds are NOT consistent across the processing unit attached to this stream.  \n  Check to insure that the units match for each indivdual compound.");
										
									}break;
									case QTY_NOT_SAME:
									{
										result = new IRule.Warning((IModelElement)msgObject, "Overall mass balance is satisfied.\n One or more of the individual mass balances across the processing unit attached to this stream is NOT satisfied. \n Check the amount of each compound entering and leaving the process unit.");
										
									}break;
									case SUCCESS: {
										result = new IRule.Success((IModelElement)msgObject);
									}break;
									default:{}break;			
								
								}
							}
						}
						else
						{
							switch(ret_type)
							{
								case UNITS_NOT_SAME:
								{
									result = new IRule.Warning((IModelElement)msgObject, "Units of all stream(s) entering or leaving the process unit connected to this stream do not match.\n Make sure that the units of all stream(s) connected to the process unit match.");
									
								}break;
								case QTY_NOT_SAME:
								{
									result = new IRule.Warning((IModelElement)msgObject, "Overall mass balance across the process unit connected to this stream is not satisfied.\n Make sure that the quantities of all incoming and outgoing streams match.");
									
								}break;
								default:{}break;							
							
							}							
						}
					}
					
				}
			}
		}
		//object = (IModelElement)msgObject;
		return result;		
		
	}
	public void SetInStreamList(ProcessUnit pu)
	{
		myInstreamList.clear();
		myOverallInstreamList.clear();
		
		Iterator<IChild> Streamitr = pu.getInStreams().iterator();
		
		while(Streamitr.hasNext())
		{
			Stream mu = (Stream)Streamitr.next();
			Iterator<Tag> StreamChilditr = mu.iterator();
			
			if(StreamChilditr!= null)
			{
				Tag tu = (Tag)StreamChilditr.next();							
				
				Iterator<IChild> Tagiterator = tu.iterator(); // For checking the type
				Iterator<IChild> Tempiterator = tu.iterator(); // For getting the value
				while(Tagiterator.hasNext())
				{
					if(Tagiterator.next() instanceof OverallTagComponent)
					{
						ITagComponent childComponent = (ITagComponent) Tempiterator.next();
						//String strlabe = childComponent.getLabel();
						myOverallInstreamList.add(childComponent);
					}
					else
					{
						TagComponentList subComponent = (TagComponentList)Tempiterator.next();
						Iterator<IGeneralTagComponent> Listiterator = subComponent.getChildren().iterator();
						ITagComponent childComponent = null;
						while(Listiterator.hasNext())
						{				
							childComponent = (ITagComponent)Listiterator.next();
							
							if(childComponent.isInitial()) break; // This is the last element which is not yet initialized
							
							String material = childComponent.getMaterial();
							if(material.equalsIgnoreCase("Select") || material.isEmpty())
							{
								continue;
							}
							myInstreamList.add(childComponent);							
						}
					}
				}
			}
		}
		
	}
	
	public void SetOutStreamList(ProcessUnit pu)
	{
		// Only this line is different from SetInstreamList()
		Iterator<IChild> Streamitr = pu.getOutStreams().iterator();
		myOutstreamList.clear();
	    myOverallOutstreamList.clear();
		
		while(Streamitr.hasNext())
		{
			Stream mu = (Stream)Streamitr.next();
			Iterator<Tag> StreamChilditr = mu.iterator();
			
			if(StreamChilditr!= null)
			{
				Tag tu = (Tag)StreamChilditr.next();							
				
				Iterator<IChild> Tagiterator = tu.iterator(); // For checking the type
				Iterator<IChild> Tempiterator = tu.iterator(); // For getting the value
				while(Tagiterator.hasNext())
				{
					if(Tagiterator.next() instanceof OverallTagComponent)
					{
						ITagComponent childComponent = (ITagComponent) Tempiterator.next();
						//String strlabe = childComponent.getLabel();
						myOverallOutstreamList.add(childComponent);
						msgObject = (AbstractTagComponent)childComponent;
					}
					else
					{
						TagComponentList subComponent = (TagComponentList)Tempiterator.next();
						Iterator<IGeneralTagComponent> Listiterator = subComponent.getChildren().iterator();
						ITagComponent childComponent = null;
						while(Listiterator.hasNext())
						{				
							childComponent = (ITagComponent)Listiterator.next();
							
							if(childComponent.isInitial()) break;
							
							String material = childComponent.getMaterial();
							if(material.equalsIgnoreCase("Select") || material.isEmpty())
							{
								continue;
							}
							myOutstreamList.add(childComponent);
						}
					}
				}
			}
		}
	}
	
	// This function checks for unit and qty consistency of individual components
	int ComponentCheck(ArrayList<IChild> Inlist, ArrayList<IChild> Outlist) {
		boolean bResult = false;
		int iResult = FAILURE;
		// if both the lists are non empty
		if( !(Inlist.isEmpty()) && !(Outlist.isEmpty()))
		{
			Iterator InputListItr = Inlist.iterator();
			int iInputIndex = 0;
			
			while(InputListItr.hasNext())
			{	
				int size = Inlist.size();
				if(iInputIndex >= size ) break;
				
				ITagComponent childComponent = (ITagComponent) InputListItr.next();
				iInputIndex++;
				String InelementName = childComponent.getMaterial();
				// Empty Element
				if(InelementName.equals("")|| childComponent.isInitial())
				{
					break;
				}
				
				String qty = childComponent.getQuantity();
				float InQty = 0.0f;				
				String InUnits = "";
				InUnits = childComponent.getUnits();
				if(!(qty.equals("?")|| (qty.equals(""))))
				{						
								
					// if the units are in % then get the actual value
					if(InUnits.equals("%"))
					{
						OverallTagComponent tmpParent = (OverallTagComponent)((IModelElement)childComponent.getParent().getParent().getChildren().get(0));
						String ParentQty = tmpParent.getQuantity();
						float iParentQty = 0.0f;
						
						if(!(ParentQty.isEmpty()|| ParentQty.equals("?")))
						{
							iParentQty = Float.valueOf(ParentQty);
							InQty = Float.valueOf(qty);
							InQty = (iParentQty * InQty)/100;
						}							
						
					}
					else
					{
						InQty = Float.valueOf(qty);
					}
				}
				
				
				Iterator InputListItr2 = Inlist.iterator();
				int iInIndex2 = 0;
				// Make the second iterator point to the same element of the first iterator
				while(InputListItr2.hasNext() && iInIndex2 < iInputIndex)
				{
					InputListItr2.next();
					iInIndex2++;
				}
				// Check if the same element has multiple occurences in the input list.
				// If it has than add all the incoming quantites of the element and
				// remove al the duplicate occurences of the element from the input list.
				while(InputListItr2.hasNext())
				{	
					
					 // Second iterator gets the next item.
					ITagComponent childComponent2 = (ITagComponent) InputListItr2.next();						
					String InelementName2 = childComponent2.getMaterial();
					if(InelementName2.equalsIgnoreCase(InelementName))
					{
						qty = childComponent2.getQuantity();
						if(!(qty.equals("?")|| (qty.equals(""))))
						{						
							InUnits = childComponent2.getUnits();
							
							// if the units are in % then get the actual value
							if(InUnits.equals("%"))
							{
								OverallTagComponent tmpParent = (OverallTagComponent)((IModelElement)childComponent2.getParent().getParent().getChildren().get(0));
								String ParentQty = tmpParent.getQuantity();
								float iParentQty = 0.0f;
								if(!(ParentQty.isEmpty()|| ParentQty.equals("?")))
								{
									iParentQty = Float.valueOf(ParentQty);
									float InQty2 = Float.valueOf(qty);
									// Add the quantity to the earlier value
									InQty += (iParentQty * InQty2)/100;
								}									
								
							}
							else
							{
								 //Add the quantity to the earlier value
								InQty += Float.valueOf(qty);
							}
						}
						// Remove the second occurence of the same element from the original input list
						Inlist.remove(iInIndex2);
						// reintialize the iterator and bring it back to the original position
						InputListItr = Inlist.iterator();
						for(int i = 0; i < iInputIndex ;i++)
						{
							InputListItr.next();
						}					
						
						InputListItr2 = Inlist.iterator();
						iInIndex2= iInputIndex;
						for(int i = 0; i < iInputIndex ;i++)
						{
							InputListItr2.next();								
						}							
					}
					iInIndex2++;
				}
				
				
				Iterator OutputListItr = Outlist.iterator();
				float iTotalOutQty = 0.0f;
				boolean bOutElementIsPresent = false;
				while(OutputListItr.hasNext())
				{
					ITagComponent OutchildComponent = (ITagComponent) OutputListItr.next();
					String OutelementName = OutchildComponent.getMaterial();
					String OutUnits = OutchildComponent.getUnits();
					
					//if element Name And Units are same, check for Qty.
					// Units check can be removed if we convert warning to error in component iterating rule.
					if(OutelementName.equals(InelementName) ) //&& (OutUnits.equals(InUnits)|| OutUnits.equals("%"))
					{
						//bOutElementIsPresent = false; // PA commented before main study
						
						// if in unit or out-unit is not % and out units is not equal to in units then break
						//if(!InUnits.equals("%") && !(OutUnits.equals(InUnits)))
						if( !(InUnits.equals("%")|| OutUnits.equals("%")) &&
								!(InUnits.equals(OutUnits))
								)
						{
							return UNITS_NOT_SAME;
						}
						String Outqty = OutchildComponent.getQuantity();
						
						if(!(Outqty.equals("?")|| (Outqty.equals(""))))
						{								
							if(OutUnits.equals("%"))
							{
								OverallTagComponent tmpParent = (OverallTagComponent)((IModelElement)OutchildComponent.getParent().getParent().getChildren().get(0));
								String ParentQty = tmpParent.getQuantity();
								float iParentQty = 0.0f;
								if(!(ParentQty.isEmpty()|| ParentQty.equals("?")))
								{
									iParentQty = Float.valueOf(ParentQty);
									float InQty2 = Float.valueOf(Outqty);									
									// Add the quantity to the earlier value
									iTotalOutQty += (iParentQty * InQty2)/100;
								}
								else{
									bOutElementIsPresent = true;
								}
							}
							else
							{
								iTotalOutQty += Float.valueOf(Outqty);
							}
						}
						else{
							bOutElementIsPresent = true;
						}
					}						
				}
				// If out quantity is equal to IN Quantity of the individual element
				// OR if element is present but qty is unknown
				if(iTotalOutQty == InQty || InQty == 0.0 || bOutElementIsPresent)
				{
					//bResult = true;	
					iResult =  SUCCESS;
				}
				else
				{
					// PA : We can prepare the string for elements not present in the out list here.
					//bResult = false;
					iResult = QTY_NOT_SAME;
					break;
				}
			}
			
		}
		return iResult;
	}
	
	int OverllAllCheck(ArrayList<IChild> Inlist, ArrayList<IChild> Outlist) {
		boolean bResult = false;
		
		boolean bInQty = false, bOutQty = false;
		float iOverAllInQty = 0;
		float iOverAllOutQty = 0;
		String OverAllInUnit = "";
		String OverAllOutUnit = "";
		// if both the lists are non empty
		if( !(Inlist.isEmpty()) && !(Outlist.isEmpty()))
		{
			Iterator InputListItr = Inlist.iterator();
			while(InputListItr.hasNext())
			{
				ITagComponent childComponent = (ITagComponent) InputListItr.next();
				String qty = childComponent.getQuantity();	
				if(OverAllInUnit.isEmpty())
				{				
					OverAllInUnit = childComponent.getUnits();
				}
				else if(!(OverAllInUnit.equals(childComponent.getUnits()))){
					
					//return bResult;
					return UNITS_NOT_SAME;
				}
				if(!(qty.equals("?")|| (qty.equals(""))) && !childComponent.isInitial())
				{
					iOverAllInQty += Float.valueOf(qty);
				}
				// if incoming  qty is unknown
				else if(!childComponent.isInitial() && qty.equals("?")){
					bInQty = true;
				}
			}
			Iterator OutputListItr = Outlist.iterator();
			while(OutputListItr.hasNext())
			{
				ITagComponent childComponent = (ITagComponent) OutputListItr.next();
				String qty = childComponent.getQuantity();
				if(OverAllOutUnit.isEmpty())
				{				
					OverAllOutUnit = childComponent.getUnits();
				}
				else if(!(OverAllOutUnit.equals(childComponent.getUnits()))){
					//return bResult;
					return UNITS_NOT_SAME;
				}
				
				if(!(qty.equals("?")|| (qty.equalsIgnoreCase(""))))
				{
					iOverAllOutQty += Float.valueOf(qty);
				}
				// if out qty is unknown
				else if(!childComponent.isInitial() && qty.equals("?")){
					bOutQty = true;
				}
			}
						
			// v1: Return different error types for units and qty mismatch
			if(! (OverAllOutUnit.equals(OverAllInUnit)))
			{
				return UNITS_NOT_SAME;
			}
			// units are same And either in-qty equal out-qty OR one of them is unknown.
			else if(OverAllOutUnit.equals(OverAllInUnit) && (iOverAllOutQty == iOverAllInQty ||(bOutQty || bInQty)))
			{
				return SUCCESS;
			}
			return QTY_NOT_SAME;
		}
			
		// if Overall Quantity and overall units are same.
		// This check is useful in case when all the input units are same and output unit is different.
		// This check might not work accurately if even the input units are not same.
		// As per discussion with RZ this case will be handled in equation check.
			/*if(iOverAllOutQty == iOverAllInQty && OverAllOutUnit.equals(OverAllInUnit))
			{
				bResult = true;
			}
			// Incoming and Outgoing units are same but quantities are unknown
			else if(OverAllOutUnit.equals(OverAllInUnit) && (bOutQty || bInQty)){
				bResult = true;
			}
		}
		return bResult;*/
		
		return SUCCESS;
	}
	// This method sets the annotation on the given model element
	public void setAnnotation(RuleResult result, String strLabel)
	{
		interpreter = ModelEnv.getInstanceFor((IModelElement)msgObject).getInterpreter();
		ModelElementData modelData = interpreter.getModelData(strLabel);
		if ( null != modelData)
		{
			if(result instanceof IRule.Success)
			{
				interpreter.removeEntry(result.getSource());		
				modelData.setErrorAnnotation(null);
								
			}
			else
			{
				String strmsg = interpreter.updateConsoleMap(result);
				modelData.setErrorAnnotation(new Annotation((IModelElement)msgObject, ProcessModelConstants.DESC_WARNING, result.getMessage(),strmsg));
			}
		}
	}
	
	private String GetStreamElementNames(ArrayList<IChild> list1)
	{
		String listofNames = " ";
		String[] arrayOfStrings = null;
		if( !(list1.isEmpty()))
		{
			int iIndex = 0;
			int iIndex1 = 0;
			Iterator ListItr1 = list1.iterator();		
			int size = list1.size();
			arrayOfStrings = new String[size];
			while(ListItr1.hasNext())
			{					
				if(iIndex >= size ) break;
				
				ITagComponent childComponent = (ITagComponent) ListItr1.next();				
				String elementName = childComponent.getMaterial();
				// Empty Element
				if(childComponent.isInitial())
				{
					break;
				}
				int i = 0;boolean bflag = false;
				// Check if the element is already present in the array
				while(i < size && null != arrayOfStrings[i])
				{
					if(arrayOfStrings[i].equals(elementName))
					{
						bflag = true;
						break;
					}
					i++;
				}
				// if element name is not present in the array add it
				if(!bflag){
					listofNames += elementName;
					listofNames += ",";
					arrayOfStrings[iIndex1] = elementName;
					iIndex1++;
				}
				iIndex++;
			}
		}
		return listofNames;
	}
	
//	 This function checks if element Present in Inlist and not present in Outlist
	 private String CheckelelementInOutlist(ArrayList<IChild> list1, ArrayList<IChild>list2) {
		//boolean bResult = false;	
		String listofNames = "";
		String bResult = null;
		// unique list of names of the elements in the list1
		String[] arrayOfStrings1 = null;
		String[] arrayOfStrings2 = null;

		int iIndex1 = 0;
		int iIndex2 = 0;
		// if both the lists are non empty
		if( !(list1.isEmpty()) && !(list2.isEmpty()))
		{
			int iIndex = 0;
			Iterator ListItr1 = list1.iterator();		
			int size = list1.size();
			arrayOfStrings1 = new String[size];
			
			while(ListItr1.hasNext())
			{					
				if(iIndex >= size ) break;
				
				ITagComponent childComponent = (ITagComponent) ListItr1.next();				
				String elementName1 = childComponent.getMaterial();
				// Empty Element
				if(childComponent.isInitial())
				{
					break;
				}
				int i = 0;boolean bflag = false;
				// Check if the element is already present in the array
				while(i < size && null != arrayOfStrings1[i])
				{
					if(arrayOfStrings1[i].equals(elementName1))
					{
						bflag = true;
						break;
					}
					i++;
				}
				// if element name is not present in the array add it
				if(!bflag){
					arrayOfStrings1[iIndex1] = elementName1;
					iIndex1++;
				}
				iIndex++;
			}
						
			Iterator ListItr2 = list2.iterator();
			iIndex = 0;
			int size2 = list2.size();			
			arrayOfStrings2 = new String[size2];
			while(ListItr2.hasNext())
			{					
				if(iIndex >= size2 ) break;				
				ITagComponent childComponent = (ITagComponent) ListItr2.next();				
				String elementName = childComponent.getMaterial();
				// Empty Element
				if(childComponent.isInitial())
				{
					break;
				}
				int i = 0;boolean bflag = false;
				// Check if the element is already present in the array
				while(i < size && null != arrayOfStrings2[i])
				{
					if(arrayOfStrings2[i].equals(elementName))
					{
						bflag = true;
						break;
					}
					i++;
				}
				// if element name is not present in the array add it
				if(!bflag){
					arrayOfStrings2[iIndex2] = elementName;
					iIndex2++;
				}
				iIndex++;
			}			
		}
		else{
			return bResult;
		}
		// if number of intems in both the lists are not same return false
		//if(iIndex2 != iIndex1){
			//return bResult;
		//}
		//else{
			
			boolean bflag = false;
			for(int i = 0;i < iIndex1 ; i++){
				bflag = false;
				for(int j = 0; j< iIndex2; j++){
					if(arrayOfStrings1[i].equals(arrayOfStrings2[j])){
						bflag = true;
						break;
					}
				}
				if(!bflag)
				{
					// enter this element in the outname list;
					listofNames += arrayOfStrings1[i];
					listofNames += ",";
					// currently just break
					//break;
				}
			}
			if(!listofNames.isEmpty()|| !listofNames.equals(""))
				bResult = listofNames;
			
		//}
		return bResult;
	 }
}// class ends

